package BuildData;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class NgangCheoTreeData {

    private ArrayList itemAr = new ArrayList();
    private ArrayList tokenAr = new ArrayList();
    private ArrayList greekAr = new ArrayList();

    public NgangCheoTreeData() throws FileNotFoundException, XMLStreamException, IOException {

        System.out.println("package BuildData; class NgangCheoTreeData; method NgangCheoTreeData");

        setNswAr();
        setItemAr();
        writeToFile();
    }

    public void setNswAr() throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream("trainset\\hand\\ngangcheo_hand.xml");
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        int atNum = xmlStreamReader.getAttributeCount();
                        if (atNum == 0) {
                            String attributeValue = xmlStreamReader.getAttributeValue(0);

                            String nsw = xmlStreamReader.getElementText();
                            Token tkn = new Token();
                            tkn.setValue(nsw);
                            tkn.setNsw(attributeValue);
                            tokenAr.add(tkn);
                        } else {
                            String atVal = xmlStreamReader.getAttributeValue(1);
                            String nsw = xmlStreamReader.getElementText();
                            Token tkn = new Token();
                            tkn.setValue(nsw);
                            tkn.setNsw(atVal);
                            tokenAr.add(tkn);
                        }

                    } else {
                        if (localName.equals("URL")) {
                            String url = xmlStreamReader.getElementText();
                        }
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();

                    StringTokenizer st = new StringTokenizer(character);
                    while (st.hasMoreTokens()) {
                        String tokenStr = st.nextToken();
                        Token token = new Token();
                        token.setValue(tokenStr);
                        token.setNsw("NOT");
                        tokenAr.add(token);

                    }

            }
        }


    }

    public void setItemAr() {
        ArrayList nswTrainSet = new ArrayList();
        for (int i = 0; i < tokenAr.size(); i++) {
            Token tkn = new Token();
            tkn = (Token) tokenAr.get(i);
            String nswStr = tkn.getValue();
            String nswClass = tkn.getNsw();
            if (isNgangCheo(nswStr)) {
                ALPHA nsw = new ALPHA();
                String tknValue = tkn.getValue();
                String tknNsw = tkn.getNsw();
                nsw.setclassNsw(tknNsw);

                nsw.settknAf1(getToken(i + 1));
                nsw.settknAf2(getToken(i + 2));
                nsw.settknBf1(getToken(i - 1));
                nsw.settknBf2(getToken(i - 2));
                nsw.setNswStr(tknValue);
                nswTrainSet.add(nsw);
            }
        }
        itemAr = nswTrainSet;
    }

    public void writeToFile() throws IOException {

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data\\tree\\ngangcheotreedata.db"), "UTF8"));
        bw.write("tree-database\n");
        bw.write("object name tknBf2 symbolic tknBf1 symbolic nswStr symbolic tknAf1 symbolic tknAf2 symbolic classNsw symbolic\n");

        ArrayList ar = new ArrayList();
        ar = getItemAr();
        String item = "";
        for (int i = 0; i < ar.size(); i++) {
            item = "";
            ALPHA nsw = (ALPHA) ar.get(i);
            item += i;


            String tknBf2 = nsw.gettknBf2();
            item += " " + tknBf2;
            String tknBf1 = nsw.gettknBf1();
            item += " " + tknBf1;
            String nswStr = nsw.getNswStr();
            item += " " + nswStr;
            String tknAf1 = nsw.gettknAf1();
            item += " " + tknAf1;
            String tknAf2 = nsw.gettknAf2();
            item += " " + tknAf2;

            String classNsw = nsw.getclassNsw();
            item += " " + classNsw;


            item += "\n";
            bw.write(item);

        }
        bw.close();

    }

    public ArrayList getItemAr() {
        return itemAr;
    }

    public int getNumCount(String nsw) {
        int count = 0;
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                count++;
            }
        }
        return count;
    }

    public String getToken(int index) {
        if (index < 0 || index >= tokenAr.size()) {
            return "";
        } else {
            Token tkn = (Token) tokenAr.get(index);
            return tkn.getValue();
        }

    }

    public boolean isNum(String nsw) {
        for (int i = 0; i < nsw.length(); i++) {
            if (Character.isDigit(nsw.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public boolean isNgangCheo(String nsw) {
        Pattern p = Pattern.compile("^[\\p{Pd}/]$");
        Matcher m = p.matcher(nsw);
        return m.find();

    }
}
